package heman.server.controllers;


import java.util.ArrayList;

import heman.common.XMLConverter;
import heman.server.entities.Player;
import heman.server.entities.Table;
import heman.server.entities.TableManager;



import ks.framework.common.Message;
import ks.framework.communicator.Communicator;
import ks.server.interfaces.IProcessServerMessage;


import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


/**
 * Turn controller handles TURN messages.
 * 
 * Example:
 * 
 * <request version="1.0" id="589a39591271844e3fbe32bbb9281ad9">
  <start table="100" /> 
  </request>
  
  Protocal
 * Now in this version, the player take the first turn may not be the moderator
 * But this can be easily implemented by change the table's startGame function to 
 * make sure the moderator is in the first place of the playerList

 * 
 * @author Wenlong Liu
 *
 */
public class GameStartController implements IProcessServerMessage {

	public boolean process(Communicator com, Message m) {
		
        //Get all the attributes of <turn> node
        NamedNodeMap map=m.contents().getAttributes();
 
    	
		StringBuilder sb= new StringBuilder(Message.responseHeader(true,m.id));
		
		String originator = m.getOriginator();
        String tableID=map.
        		getNamedItem("table").getNodeValue();
        Table table=TableManager.getInstance().getTable(tableID);
        if (table==null ) return false; 
      //Make sure the sender is the moderator of that table
    	if (!table.getModerator().getId().equals(m.getOriginator())) return false;
    	
    	//do some initialization
    	if (!table.startGame()) return false;
    	
        ArrayList<String> players=table.getPlayerIDs();
        //Get the move
        
        sb.append("<gameResponse table=\"");
        sb.append(tableID);
        sb.append("\" turn=\"");
        
		//Send this gameResponse to the present player
		sb.append(table.getNextPlayer().getId());
		
        sb.append("\"");
        
     
        	sb.append(" move=\"");
            sb.append("\"");
            sb.append(" from=\"");
            sb.append(originator);
            sb.append("\">");
            
            //Add the game players
            //<gameState player="12995" score="0"/>
               
            for(String s :  table.getPlayerIDs()){
            	sb.append("<gameState player = \"" + s 
            			+ "\" score=\"0\"/>");
            }
            
       
       
        sb.append("</gameResponse></response>");
        
        Document d = Message.construct(sb.toString());
        Message r = new Message (d);
        r.setOriginator(originator);
        if (!com.distribute(players.iterator(), r)) return false;
        
        
        //Send the output to all players
        sb = new StringBuilder(Message.responseHeader(true));
        sb.append("<output system='true'><text>");
        sb.append("A game of").append(table.getGame()).append(" has started at Table "+tableID);
        sb.append("</text></output></response>");
        d = Message.construct (sb.toString());
        
        // send to all clients...
        r = new Message (d);
        r.setBroadcast();
        
        return com.distribute(r);
        

        
        

	}

}
